<?php
    class Posts_model extends CI_Model {
        
        function __construct() {
            parent::__construct();
        }
        
        function get_last_ten_posts(){
            $this->db->order_by('posts_id', 'desc');
            $query = $this->db->get('posts', 20);
            return $query->result_array();
        }
                
        function get_last_ten_parts(){
            $this->db->order_by('pparts_id', 'desc');
            $query = $this->db->get('posts_parts', 20);
            return $query->result_array();
        }
        
        function get_parts() {
            $this->db->order_by('pparts_name', 'asc');
            $query = $this->db->get('posts_parts');
            $content = $query->result_array();
            foreach ($content as $row)
            {
                $data[$row['pparts_id']] = $row['pparts_name'];
            }
            return $data;
        }
        
        function insert_one_parts($data) {
            $this->db->insert('posts_parts', $data);
        }
        
        function insert_one_posts($data) {
            $this->db->insert('posts', $data);
        }
        
        function get_one_posts($id)
        {
            $this->db->where('posts_id', $id);
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        
        function get_one_parts($id)
        {
            $this->db->where('pparts_id', $id);
            $query = $this->db->get('posts_parts');
            return $query->result_array();
        }
        
        function update_one_posts($id, $data) {
            $this->db->where('posts_id', $id);
            $this->db->update('posts', $data);
        }
        
        function update_one_parts($id, $data) {
            $this->db->where('pparts_id', $id);
            $this->db->update('posts_parts', $data);
        }
        
        function update_posts_in_parts($id) {
            $this->db->where('pparts_id', $id);
            $query = $this->db->get('posts_parts');
            $content = $query->result_array();
            $number = 1 + $content['0']['pparts_posts_count'];
            $data = array('pparts_posts_count' => $number);
            $this->db->where('pparts_id', $id);
            $this->db->update('posts_parts', $data);
        }
        
        function drop_one_posts($id) {
            $this->db->where('posts_id', $id);
            $this->db->delete('posts');
        }
        
        function drop_one_parts($id) {
            $this->db->where('pparts_id', $id);
            $this->db->delete('posts_parts');
        }
        
        function drop_posts_where_parts($id) {
            $this->db->where('posts_parent', $id);
            $this->db->delete('posts');
        }
        
        function drop_posts_in_parts($id) {
            $this->db->where('pparts_id', $id);
            $query = $this->db->get('posts_parts');
            $content = $query->result_array();
            $number = $content['0']['pparts_posts_count'] - 1;
            $data = array('pparts_posts_count' => $number);
            $this->db->where('pparts_id', $id);
            $this->db->update('posts_parts', $data);
        }
    }
?>
